package com.laozhao.framework.snow.view;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.laozhao.framework.snow.AreaModel;
import com.laozhao.framework.snow.PageModel;

/**
 * -------------------------------------------------<br>
 * |MenuBar<br>
 * -------------------------------------------------<br>
 * |ToolBar                    |right navigation<br>
 * ----------------------------|<br>
 * |message okBtn cancelBtn    |<br>
 * ----------------------------|<br>
 * |search panel               |<br>
 * ----------------------------|<br>
 * |view panel                 |<br>
 * ----------------------------|<br>
 * |editor panel               |<br>
 * --------------------------------------------------<br>
 * @author zdongmen
 *
 */
public abstract class AbstractPanelPage extends JPanel implements Page{

	

	/**
	 * 
	 */
	private static final long serialVersionUID = 489139483554371720L;

	protected PageModel pageModel;
	protected Map<String, Object> paramValueMap = new HashMap<String, Object>();
	protected Map<String, Object> paramLabelMap = new HashMap<String, Object>();
	protected Map<String, Method> paramMethodMap = new HashMap<String, Method>();
	
	// menuBar
	protected JComponent menuBar;	
	// toolBar
	protected JComponent toolBar;
	// search panel
	protected JComponent searchPanel;
	// view
	protected JComponent viewPanel;
	// editor
	protected JComponent editorPanel;
	// navigation
	protected JComponent navigationBar;
	
	// message
	protected JComponent messageBar;
	

	// action
	protected ActionListener checkListener;
	protected ActionListener eventListener;

	protected String id;
	protected Icon icon;
	
	protected Toolkit toolkit = Toolkit.getDefaultToolkit();
	
	public AbstractPanelPage(){
		super();
		
	}
	
	public AbstractPanelPage(PageModel pageModel){
		this.pageModel = pageModel;
	}


	@Override
	public void build(PageModel pageModel){
		this.pageModel = pageModel;
		if (pageModel != null) {
//			setTitle(pageModel.getName());
			setId(pageModel.getId());
			setIcon(new ImageIcon(toolkit.createImage(pageModel.getIcon())));
//			setToolTipText(pageModel.getDescription());
		}
//		// init
		Map<String, AreaModel> areas = pageModel.getAreas();
		if (areas != null && areas.size() > 0) {
			for (String key : areas.keySet()) {
				if (key.startsWith("menubar")) {
					setMenuBar(areas.get(key));
				}else if (key.startsWith("editor")) {
					setEditorPanel(areas.get(key));
				}else if (key.startsWith("navigation")) {
					setNavigationBar(areas.get(key));
				}else if (key.startsWith("search")) {
					setSearchPanel(areas.get(key));
				}else if (key.startsWith("view")) {
					setViewPanel(areas.get(key));
				}else if (key.startsWith("toolbar")) {
					setToolBar(areas.get(key));
				}else if (key.startsWith("message")) {
					setMessageBar(areas.get(key));
				}
			}
		}
		
		this.setLayout(new BorderLayout(5,5));

		JComponent temComponent = getMenuBar();			
		if (temComponent != null) {
			this.add(temComponent, BorderLayout.NORTH);
		}
		JPanel toolsPanel = new JPanel(new BorderLayout(5,5));
		this.add(toolsPanel, BorderLayout.CENTER);
		temComponent = getToolBar();
		if (temComponent != null) {
			toolsPanel.add(temComponent , BorderLayout.NORTH);
		}
		temComponent = getNavigationBar();
		if (temComponent != null) {
			toolsPanel.add(temComponent , BorderLayout.WEST);
		}
		JPanel messesPanel = new JPanel(new BorderLayout(5,0));
		toolsPanel.add(messesPanel, BorderLayout.CENTER);
		
		messesPanel.add(messageBar,BorderLayout.NORTH);
		JPanel mainPanel = new JPanel(new BorderLayout(5,0));
		messesPanel.add(mainPanel,BorderLayout.CENTER);
		
		temComponent = getSearchPanel();
		if (temComponent != null) {
			mainPanel.add(temComponent,BorderLayout.NORTH);
		}
		temComponent = getViewPanel();
		if (temComponent != null) {
			JScrollPane viewsScrollPane = new JScrollPane(temComponent);
			mainPanel.add(viewsScrollPane,BorderLayout.CENTER);
		}
		temComponent = getEditorPanel();
		if (temComponent != null) {
			mainPanel.add(temComponent,BorderLayout.SOUTH);
		}
	}
	
//	public String getMessage() {
//		return message;
//	}
//
//	public void setMessage(String message) {
//		this.message = message;
//		if (message != null && "".equals(message)) {
//			messageLabel.setText(message);
//			messageBar.setVisible(true);
//		}else {
//			messageBar.setVisible(false);
//		}
//	}
//	
//
//	public String getMessageType() {
//		return messageType;
//	}

//	public void setMessageType(String messageType) {
//		this.messageType = messageType;
//		if (PAGE_MESSAGE_WARNING.equals(messageType)) {
//			messageBar.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.YELLOW,Color.RED ));
//		}else if (PAGE_MESSAGE_QUESTION.equals(messageType)) {
//			messageBar.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.YELLOW,Color.GREEN ));
//		}else if (PAGE_MESSAGE_ERROR.equals(messageType)) {
//			messageBar.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.RED,Color.GRAY ));
//		}else {
//			messageBar.setBorder(null);
//		}
//	}

	@Override
	public Map<String, Object> getValues() {
		if (paramMap != null) {
			return paramMap;
		}
		return null;
	}

	@Override
	public void setValues(Map<String, Object> params) {
		
	}

	public ActionListener getCheckListener() {
		return checkListener;
	}

	public void setCheckListener(ActionListener checkListener) {
		this.checkListener = checkListener;
	}
	

	public ActionListener getEventListener() {
		return eventListener;
	}

	public void setEventListener(ActionListener eventListener) {
		this.eventListener = eventListener;
	}

//	public String getTitle() {
//		return title;
//	}
//
//	public void setTitle(String title) {
//		this.title = title;
//	}

	public Icon getIcon() {
		return icon;
	}

	public void setIcon(Icon icon) {
		this.icon = icon;
	}
	
//	private void initMessagelbar() {
//		messageBar = new JPanel(new BorderLayout(5,0));
//		messageLabel = new JLabel();
//		messageBar.add(messageLabel,BorderLayout.CENTER);
//		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
//		buttonPanel.add(questionConfirmButton);
//		buttonPanel.add(questionCanelButton);
//		messageBar.add(buttonPanel,BorderLayout.EAST);
//	}

//	public void setNavigationBar(JComponent navigationBar) {
//		this.navigationBar = navigationBar;
//	}
//	public JComponent getNavigationBar() {
//		return navigationBar;
//	}
//	
//	public void setSearchPanel(JComponent searchPanel) {
//		this.searchPanel = searchPanel;
//	}
//	public JComponent getSearchPanel() {
//		return searchPanel;
//	}
//	
//	public void setViewPanel(JComponent viewPanel) {
//		this.viewPanel = viewPanel;
//	}
//	public JComponent getViewPanel() {
//		return viewPanel;
//	}
//	
//	public void setEditorPanel(JComponent editorPanel) {
//		this.editorPanel = editorPanel;
//	}
//	public JComponent getEditorPanel() {
//		return editorPanel;
//	}
//
//	public JComponent getMenuBar() {
//		return menuBar;
//	}
//
//	public void setMenuBar(JComponent menuBar) {
//		this.menuBar = menuBar;
//	}
//
//	public JComponent getToolBar() {
//		return toolBar;
//	}
//
//	public void setToolBar(JComponent toolBar) {
//		this.toolBar = toolBar;
//	}
//
//	public MessageArea getMessageBar() {
//		return messageBar;
//	}
//
//	public void setMessageBar(MessageArea messageBar) {
//		this.messageBar = messageBar;
//	}
	
//	@Override
//	public PageModel getPageModel() {
//		return pageModel;
//	}
//
//	@Override
//	public void setPageModel(PageModel pageModel) {
//		this.pageModel = pageModel;
//	}

	public abstract void setNavigationBar(AreaModel navigationBar);
	
	public JComponent getNavigationBar() {
		return navigationBar;
	}
	
	public abstract void setSearchPanel(AreaModel searchPanel) ;
	public JComponent getSearchPanel() {
		return searchPanel;
	}
	
	public abstract void setViewPanel(AreaModel viewPanel);
	public JComponent getViewPanel() {
		return viewPanel;
	}
	
	public abstract  void setEditorPanel(AreaModel editorPanel);
	public JComponent getEditorPanel() {
		return editorPanel;
	}

	public JComponent getMenuBar() {
		return menuBar;
	}

	public abstract void setMenuBar(AreaModel menuBar);

	public JComponent getToolBar() {
		return toolBar;
	}

	public abstract void setToolBar(AreaModel toolBar);

	public JComponent getMessageBar() {
		return messageBar;
	}

	public abstract void setMessageBar(AreaModel messageBar);

	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
}
